using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

namespace Cfg.ConfigProviders.DotNet.Xml
{
    [XmlRoot("AppSettings")]
    public class DotNetConfigFile : IEnumerable
    {
        private Hashtable _index = new Hashtable();

        public string this[string key]
        {
            get
            {
                key = key.ToLower();
                if (_index.ContainsKey(key))
                    return ((KeyValuePair)_index[key]).Value;

                return null;
            }
            set
            {
                _index[key.ToLower()] = new KeyValuePair { Key = key, Value = value};
            }
        }

        public int Count { get { return _index.Count; } }

        [XmlArrayItem("add")]
        public KeyValuePair[] KeyValuePairs
        {
            get
            {
                return _index.Values.OfType<KeyValuePair>().ToArray();
            }
            set
            {
                _index.Clear();
                foreach (var keyValuePair in value)
                {
                    _index[keyValuePair.Key] = keyValuePair;
                }
            }
        }

        public IEnumerator GetEnumerator()
        {
            return KeyValuePairs.GetEnumerator();
        }
    }
}